;;;
;;; JaCoLiB
;;; Copyright (C) 2008 Alessandro Serra
;;; 
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;; 
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;; 
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;

;;;; Created on 2008-07-25 19:52:25

(in-package :jacolib)

;
; RPC Channel
;

(defclass rpc-channel ()
  ((rpc-server-stub
    :TYPE rpc-server-stub
    :READER rpc-channel-server-stub
    :INITFORM (make-instance 'rpc-server-stub))
   (rpc-client-stub
    :TYPE rpc-client-stub
    :READER rpc-channel-client-stub
    :INITFORM (make-instance 'rpc-client-stub))
   (rpc-client 
    :TYPE rpc-client
    :ACCESSOR rpc-channel-client)
   (quit-handler
    :INITARG :quit-handler
    :ACCESSOR rpc-channel-quit-handler)
   (quit%-handler
    :INITARG :quit%-handler
    :ACCESSOR rpc-channel-quit%-handler))
  (:documentation "A RPC Channel."))

; initializer

(defmethod initialize-instance :after ((channel rpc-channel) &rest initargs)
  (declare (ignore initargs))
  (setf (rpc-client-stub-quit-handler (rpc-channel-client-stub channel)) #'(lambda () (rpc-channel-quit channel)))
  (setf (rpc-server-stub-callback-client-stub (rpc-channel-server-stub channel)) (rpc-channel-client-stub channel)))

(defun rpc-channel-set-exec-handler (channel exec-handler)
  (setf (rpc-client-stub-exec-handler (rpc-channel-client-stub channel)) exec-handler))
  
(defun rpc-channel-set-quit-handler (channel quit-handler)
  (setf (rpc-channel-quit-handler channel) quit-handler))

(defun rpc-channel-set-quit%-handler (channel quit-handler)
  (setf (rpc-channel-quit%-handler channel) quit-handler))

(defun rpc-channel-set-server (channel rpc-server)
  (setf (rpc-server-stub-server (rpc-channel-server-stub channel)) rpc-server)
  (setf (rpc-server-quit-handler rpc-server) 
        #'(lambda () (rpc-channel-quit channel))))

(defun rpc-channel-set-client (channel rpc-client)
  (setf (rpc-channel-client channel) rpc-client))

; quit&quiting

(defun rpc-channel-quit (channel)
  (funcall (rpc-channel-quit-handler channel))
  (rpc-channel-quit% channel))

(defgeneric rpc-channel-quit% (channel)
  (:method ((channel rpc-channel))
    (funcall (rpc-channel-quit%-handler channel))
    (rpc-client-quit% (rpc-channel-client channel))
    (rpc-server-stub-quit% (rpc-channel-server-stub channel))
    (setf (slot-value channel 'rpc-server-stub) nil)
    (setf (slot-value channel 'rpc-client-stub) nil)
    (setf (slot-value channel 'quit-handler) nil)
    (setf (slot-value channel 'quit%-handler) nil)))
    
